<?php
/*
  $Id$

  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com

  Copyright (c) 2010 osCommerce

  Released under the GNU General Public License
*/

  class bm_search_results {
    var $code = 'bm_search_results';
    var $group = 'boxes';
    var $title;
    var $description;
    var $sort_order;
    var $enabled = false;

    function bm_search_results() {
      $this->title = MODULE_BOXES_SEARCH_RESULTS_TITLE;
      $this->description = MODULE_BOXES_SEARCH_RESULTS_DESCRIPTION;

      if ( defined('MODULE_BOXES_SEARCH_RESULTS_STATUS') ) {
        $this->sort_order = MODULE_BOXES_SEARCH_RESULTS_SORT_ORDER;
        $this->enabled = (MODULE_BOXES_SEARCH_RESULTS_STATUS == 'True');
				
				if (!strstr($_SERVER['PHP_SELF'], FILENAME_ADVANCED_SEARCH_RESULT))
				{
					$this->enabled = false;
				}

        $this->group = ((MODULE_BOXES_SEARCH_RESULTS_CONTENT_PLACEMENT == 'Left Column') ? 'boxes_column_left' : 'boxes_column_right');
      }
    }

    function getData() {
      global $from_str, $where_str, $languages_id;


// GET RELAVANT CATEGORIES
			$categories_string = '';
			if (!isset($_GET['categories_id']))
			{
				$category_sql = "SELECT c.categories_id, cd.categories_name, count(p.products_id) as total " . $from_str . $where_str . " group by c.categories_id order by cd.categories_name";
				$category_query = tep_db_query($category_sql);
	
				while ($category = tep_db_fetch_array($category_query))
				{
					 $categories_string .= '<a href="' . tep_href_link(FILENAME_ADVANCED_SEARCH_RESULT, tep_get_all_get_params(array('categories_id')) . 'categories_id=' . $category['categories_id']) . '">' . $category['categories_name'] . '</a> (' . $category['total'] . ')<br />';
				}
			}
			else
			{
				$category_query = tep_db_query("SELECT categories_name FROM " . TABLE_CATEGORIES_DESCRIPTION . " WHERE categories_id = '" . (int)$_GET['categories_id'] . "' and language_id = '" . (int)$languages_id . "'");
				if($category = tep_db_fetch_array($category_query))
				{
					$categories_string = '<a href="' . tep_href_link(FILENAME_ADVANCED_SEARCH_RESULT, tep_get_all_get_params(array('categories_id'))) . '">[X] ' . $category['categories_name'] . '</a>';
				}

				
			}


// GET RELAVANT MANUFACTURERS
			$manufacturer_string = '';
			if (!isset($_GET['manufacturers_id']))
			{
				$manufacturer_sql = "SELECT m.manufacturers_id, m.manufacturers_name, count(p.products_id) as total " . $from_str . $where_str . " group by m.manufacturers_id order by m.manufacturers_name";
				$manufacturer_query = tep_db_query($manufacturer_sql);
	
	
				while ($manufacturer = tep_db_fetch_array($manufacturer_query))
				{
					 $manufacturer_string .= '<a href="' . tep_href_link(FILENAME_ADVANCED_SEARCH_RESULT, tep_get_all_get_params(array('manufacturers_id')) . 'manufacturers_id=' . $manufacturer['manufacturers_id']) . '">' . $manufacturer['manufacturers_name'] . '</a> (' . $manufacturer['total'] . ')<br />';
				}
			}
			else
			{
				$manufacturer_query = tep_db_query("SELECT manufacturers_name FROM " . TABLE_MANUFACTURERS . " WHERE manufacturers_id = '" . (int)$_GET['manufacturers_id'] . "'");
				if($manufacturer = tep_db_fetch_array($manufacturer_query))
				{
					$manufacturer_string = '<a href="' . tep_href_link(FILENAME_ADVANCED_SEARCH_RESULT, tep_get_all_get_params(array('manufacturers_id'))) . '">[X] ' . $manufacturer['manufacturers_name'] . '</a>';
				}

				
			}

// GET PRICE RANGES
$price_string = '';
				$price_range[] = array(
					'total' => 0,
					'text' => 'Under $100',
					'pfrom' => 0,
					'pto' => 100
				);
				$price_range[] = array(
					'total' => 0,
					'text' => '$100 - $200',
					'pfrom' => 100,
					'pto' => 200
				);
				$price_range[] = array(
					'total' => 0,
					'text' => 'Over $200',
					'pfrom' => 200,
					'pto' => ''
				);
if (!isset($_GET['pfrom']) && !isset($_GET['pto']))
{
				$price_query = tep_db_query("SELECT DISTINCT p.products_id, p.products_price " . $from_str . $where_str);

				while ($price = tep_db_fetch_array($price_query))
				{
					for($i=0; $i<sizeof($price_range); $i++)
					{
						if ($price_range[$i]['pto']!='' && $price_range[$i]['pfrom']!='')
						{
							if ($price['products_price'] < $price_range[$i]['pto'] && $price['products_price'] > $price_range[$i]['pfrom'])
							{
								$price_range[$i]['total']++;
								break;
							}
						}
						else if ($price_range[$i]['pto']!='')
						{
							if ($price['products_price'] < $price_range[$i]['pto'])
							{
								$price_range[$i]['total']++;
								break;
							}
						}
						else if ($price_range[$i]['pfrom']!='')
						{
							if ($price['products_price'] > $price_range[$i]['pfrom'])
							{
								$price_range[$i]['total']++;
								break;
							}
						}
					}
				}
				foreach ($price_range as $price)
				{
					$and = '';
					$price_paramaters = '';
					if ($price['pfrom']!='')
					{
						$and = '&';
						$price_paramaters .= 'pfrom=' . $price['pfrom'];
					}
					if ($price['pto']!='')
					{
						$price_paramaters .= $and . 'pto=' . $price['pto'];
					}
					
					$price_string .= '<a href="' . tep_href_link(FILENAME_ADVANCED_SEARCH_RESULT, tep_get_all_get_params(array('pfrom','pto')) . $price_paramaters) . '">' . $price['text'] . '</a> (' . $price['total'] . ')<br />';
				}
}
else
{
	foreach($price_range as $price)
	{
		if ($price['pfrom'] == $_GET['pfrom'] && $price['pto'] == $_GET['pto'])
		{
			break;
		}
	}
	$price_string = '<a href="' . tep_href_link(FILENAME_ADVANCED_SEARCH_RESULT, tep_get_all_get_params(array('pfrom', 'pto'))) . '">[X] ' . $price['text'] . '</a>';
}
      $data = '<div class="ui-widget infoBoxContainer">' .
              '  <div class="ui-widget-header infoBoxHeading">' . MODULE_BOXES_SEARCH_RESULTS_BOX_TITLE . '</div>';

			if ($categories_string!='')
			{
       $data .= '  <div class="ui-widget-content infoBoxContents"><strong>' . MODULE_BOXES_SEARCH_RESULTS_CATEGORY . '</strong><br />' . $categories_string . '</div>';
			}
			
			if ($manufacturer_string!='')
			{
       $data .=  '  <div class="ui-widget-content infoBoxContents"><strong>' . MODULE_BOXES_SEARCH_RESULTS_MANUFACTURER . '</strong><br />' . $manufacturer_string . '</div>';
			}
			
			if ($price_string!='')
			{
       $data .=  '  <div class="ui-widget-content infoBoxContents"><strong>' . MODULE_BOXES_SEARCH_RESULTS_PRICE . '</strong><br />' . $price_string . '</div>';
			}
			
			$data .= '</div>';

      return $data;
    }

    function execute() {
      global $SID, $oscTemplate;

      //if ((USE_CACHE == 'true') && empty($SID)) {
      //  $output = tep_cache_categories_box();
      //} else {
        $output = $this->getData();
      //}

      $oscTemplate->addBlock($output, $this->group);
    }

    function isEnabled() {
      return $this->enabled;
    }

    function check() {
      return defined('MODULE_BOXES_SEARCH_RESULTS_STATUS');
    }

    function install() {
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Enable Categories Module', 'MODULE_BOXES_SEARCH_RESULTS_STATUS', 'True', 'Do you want to add the module to your shop?', '6', '1', 'tep_cfg_select_option(array(\'True\', \'False\'), ', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Content Placement', 'MODULE_BOXES_SEARCH_RESULTS_CONTENT_PLACEMENT', 'Left Column', 'Should the module be loaded in the left or right column?', '6', '1', 'tep_cfg_select_option(array(\'Left Column\', \'Right Column\'), ', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Sort Order', 'MODULE_BOXES_SEARCH_RESULTS_SORT_ORDER', '0', 'Sort order of display. Lowest is displayed first.', '6', '0', now())");
    }

    function remove() {
      tep_db_query("delete from " . TABLE_CONFIGURATION . " where configuration_key in ('" . implode("', '", $this->keys()) . "')");
    }

    function keys() {
      return array('MODULE_BOXES_SEARCH_RESULTS_STATUS', 'MODULE_BOXES_SEARCH_RESULTS_CONTENT_PLACEMENT', 'MODULE_BOXES_SEARCH_RESULTS_SORT_ORDER');
    }
  }
?>
